﻿using System;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Net;
using System.Text;
using System.Xml;
using System.Collections.Generic;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Types;

#if WORKFLOWEDITOR
using FRL.DebtChased.WorkflowEditor;
#endif // #if WORKFLOWEDITOR

namespace FRL.DebtChased.WorkflowObjects
{

	/// <summary>
	/// Declaration of WorkflowAction_SendLetterFax class
	/// Workflow action for sending a letter of fax (with or without email copy)
	/// </summary>
	public class WorkflowAction_SendLetterFax : WorkflowAction_Base
	{

		/////////////////////////////////////////////////////////////
		// Enumerations

		[Flags]
		public enum eTravelBy
		{
			NotSet	= 0x00,
			Post	= 0x01,
			Email	= 0x02,
			Fax		= 0x04,
		}

		public enum eContent
		{
			NotSet = 0,
			Template = 1,
			Resend = 2,
		}

		/////////////////////////////////////////////////////////////
		// Constants

		public const string XmlElementName = "WorkflowAction_SendLetterFax";

		public const string MainBodyTag = "Letter";
		public const string CoverBodyTag = "Cover";

		/////////////////////////////////////////////////////////////
		// Attributes

		private eTravelBy m_TravelBy = eTravelBy.NotSet;
		public eTravelBy TravelBy
		{
			get { return m_TravelBy; }
			set { m_TravelBy = value; }
		}

		private Definitions.eCommTargetType m_Target = Definitions.eCommTargetType.NotSet;
		public Definitions.eCommTargetType Target
		{
			get { return m_Target; }
			set { m_Target = value; }
		}

		private CommBodyWrapper m_commBodyWrapper = new CommBodyWrapper();
		public CommBodyWrapper CommBody
		{
			get { return m_commBodyWrapper; }
		}

		private int m_iNewLetterTypeId;
		public int NewLetterTypeId
		{
			get { return m_iNewLetterTypeId; }
			set { m_iNewLetterTypeId = value; }
		}

		private int m_iResendLetterTypeId;
		public int ResendLetterTypeId
		{
			get { return m_iResendLetterTypeId; }
			set { m_iResendLetterTypeId = value; }
		}

		private eContent m_Content = eContent.NotSet;
		public eContent Content
		{
			get { return m_Content; }
			set { m_Content = value; }
		}

		private CommBodyWrapper m_coverCommBodyWrapper = new CommBodyWrapper();
		public CommBodyWrapper CoverCommBody
		{
			get { return m_coverCommBodyWrapper; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public WorkflowAction_SendLetterFax()
		{

			// Initialize member variables
			base.Priority = ePriority.Automated;

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		protected override string _getElementName()
		{
			return XmlElementName;
		}

#if WORKFLOWEDITOR
		public override string GetBoingLabel()
		{

			// Use the first method of travel
			if ((m_TravelBy & eTravelBy.Post) == eTravelBy.Post)
				return "Letter";
			else if ((m_TravelBy & eTravelBy.Fax) == eTravelBy.Fax)
				return "Fax";
			else if ((m_TravelBy & eTravelBy.Email) == eTravelBy.Email)
				return "Email";
			else
				return "Letter";

		}
#endif // #if WORKFLOWEDITOR

#if WORKFLOWEDITOR
		public override string[] GetBoingSubStrings()
		{

			// Declare return variable
			string[] Ftmp = new string[2];

			// Build travel list
			List<string> strTravelBy = new List<string>();
			if ((m_TravelBy & eTravelBy.Post) == eTravelBy.Post)
				strTravelBy.Add("Letter");
			if ((m_TravelBy & eTravelBy.Fax) == eTravelBy.Fax)
				strTravelBy.Add("Fax");
			if ((m_TravelBy & eTravelBy.Email) == eTravelBy.Email)
				strTravelBy.Add("Email");

			// Row 0, method and target
			Ftmp[0] = string.Format("{0} to {1}", strTravelBy.ToAndList(), m_Target);

			// Row 1, template
			StringBuilder sb = new StringBuilder();
			if (m_Content == eContent.Template)
			{
				if (m_commBodyWrapper.IsEmpty)
					sb.Append("[no body]");
				else
					sb.Append(CachedData.Singleton.GetCommBodyDescription(m_commBodyWrapper));
				if(m_iNewLetterTypeId != 0)
					sb.AppendFormat(" -> \"{0}\"", CachedData.Singleton.GetLetterTypeName(m_iNewLetterTypeId));
			}
			else
				sb.AppendFormat("Re-send \"{0}\"", CachedData.Singleton.GetLetterTypeName(m_iResendLetterTypeId));
			Ftmp[1] = sb.ToString();

			// Return variable
			return Ftmp;

		}
#endif // #if WORKFLOWEDITOR

		protected override void _writeParameters(XmlWriter w)
		{

			// Write members as parameters
			w.WriteAttributeString("travelBy",((int)m_TravelBy).ToString());
			w.WriteAttributeString("target",((int)m_Target).ToString());
			w.WriteAttributeString("newLetterTypeId",m_iNewLetterTypeId.ToString());
			w.WriteAttributeString("resendLetterTypeId", m_iResendLetterTypeId.ToString());
			w.WriteAttributeString("content",((int)m_Content).ToString());

			// Write elements
			m_commBodyWrapper.Write( w, MainBodyTag );
			m_coverCommBodyWrapper.Write( w, CoverBodyTag );

		}

		protected override void _readParameters(XmlReader r)
		{

			// Read parameters
			m_TravelBy = (eTravelBy)r.SafeReadIntAttribute("travelBy", (int)eTravelBy.NotSet);
			m_Target = (Definitions.eCommTargetType)r.SafeReadIntAttribute("target", (int)Definitions.eCommTargetType.Debtor);
			m_iNewLetterTypeId = r.SafeReadIntAttribute("newLetterTypeId", 0);
			m_iResendLetterTypeId = r.SafeReadIntAttribute("resendLetterTypeId", 0);
			m_Content = (eContent)r.SafeReadIntAttribute("content", (int)eContent.Template);

			// Correct the priority
			base.Priority = ePriority.Automated;

			// Loop through elements
			if (!r.IsEmptyElement)
			{

				// Loop through elements
				while (r.Read())
				{

					// If this is an element...
					if (r.NodeType == XmlNodeType.Element)
					{

						// Compare names
						if (r.Name == CommBodyWrapper.ElementName)
						{
							string strTag = r.GetAttribute("tag");
							if (strTag == MainBodyTag)
								m_commBodyWrapper.Read(r);
							else if (strTag == CoverBodyTag)
								m_coverCommBodyWrapper.Read(r);
							else
							{
#if DEBUG
								Debug.Assert(false);
#endif // #if DEBUG
								r.SkipXmlElement();
							}
						}
						else
						{
#if DEBUG
							Debug.Assert(false);
#endif // #if DEBUG
							r.SkipXmlElement();
						}

					}
					else
						break;

				} // while-loop

			}

		}

		public override void GetObjectsUsed(List<int> iCommBodiesUsed, List<int> iPopupsUsed, List<int> iLetterTypeIds)
		{

			// Add the comm-body ids
			iCommBodiesUsed.AddUniqueNonZeroValue(m_commBodyWrapper.CommBodyId);
			iCommBodiesUsed.AddUniqueNonZeroValue(m_coverCommBodyWrapper.CommBodyId);

			// Add the letter-type ids
			iLetterTypeIds.AddUniqueNonZeroValue(m_iNewLetterTypeId);
			iLetterTypeIds.AddUniqueNonZeroValue(m_iResendLetterTypeId);

		}

		public override string GetSummary(Workflow.eSummaryFormat SummaryFormat)
		{

			// Declare return variable
			string strFtmp = null;

			// Compare formats
			if (SummaryFormat == Workflow.eSummaryFormat.Audit)
			{
				strFtmp = string.Format("{0} {1}", m_TravelBy, m_Target);
			}
			else if (SummaryFormat == Workflow.eSummaryFormat.Clockwork)
			{
				strFtmp = string.Format("{0} {1}", m_TravelBy, m_Target);
			}
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

			// Return variable
			return strFtmp;

		}

#if REPOSITORY
		public override List<AccountNote> Process(DebtChasedEntities ent, Account acc, AccountInvoice inv)
		{

			// Declare return variable
			List<AccountNote> Ftmp = null;

			// Return variable
			return Ftmp;

		}
#endif // #if REPOSITORY

	}

}
